#ifndef __LCD_DISPLAY
#define __LCD_DISPLAY

#include "main.h"

#define PAGE_WIDTH    100  //page宽度
#define PAGE_HIGHT    20   //page高度

extern uint8_t soc_flag;

/* 页面编号 */
enum lcd_display_page {
    PAGE1 = 1,
    PAGE2 = 2,
    PAGE3 = 3,
    PAGE3_2 = 4,
};

enum bms_status_en {
    NORMAL = 0, // 正常状态 无保护
    COV = 1,    // bit0 单体过压保护 Cell Block Over-Vol
    CUV = 2,    // bit1 单体欠压保护 Cell Block Under-Vol
    POV = 3,    // bit2 整组过压保护 Battery Over-Vol
    PUV = 5,    // bit3 整组欠压保护 Battery Under-Vol
    COT = 4,    // bit4 充电过温保护 Charging Over-temp
    CUT = 6,    // bit5 充电低温保护 Charging Low-temp
    DOT = 7,    // bit6 放电过温保护 Discharging Over-temp
    DUT = 8,    // bit7 放电低温保护 Discharging Low-temp
    COC = 9,    // bit8 充电过流保护 Charging Over-current
    DOC = 10,   // bit9 放电过流保护 Discharging Over-current
    SC = 11,    // bit10 短路保护 Short Circuit
    // FICE =12,   // bit11 前端检测 IC 错误 Fore-end IC Erro
    LOCK = 12,  // bit12 软件锁定 MOS Software Lock-in
    // bit13~bit15 预留 Reserve
};

enum soc_status_en {
    STANDBY = 0,
    CHARGING = 1,
    DISCHARGING = 2,
};
/* 从接口收到数据 */
enum comm_state_en {
    ERR = 0,
    UART0 = 1,
    UART2 = 2,
    CAN = 3,
};

/* LCD刷新事件 */
enum lcd_refresh_event {
    REFRESH_NONE = 0,  //
    REFRESH_BACK = 1,  //显示背景
    REFRESH_DATA = 2,  //显示数据
};

/* LCD要显示数据 */
struct lcd_display_info {
    //page1
    enum comm_state_en comm_state;  //连接ok的接口
    uint16_t pack_v;
    int32_t pack_c;
    int16_t pack_t;
    uint16_t soc;
    uint16_t designed_cap;
    enum bms_status_en bms_state;
    uint32_t bms_warn_status;  //告警状态位
    uint32_t bms_protect_status;  //保护状态位
    uint32_t bms_status;  //bms状态位
    double env_temp;
    enum soc_status_en soc_status;
    double time;        // 预计充满/放空时间
    char bms_soft_ver[19];    // unsigned char类型

    //page2
    uint16_t remain_cap;
    uint16_t cycle_cnt;

    double max_temp;
    double min_temp;

    uint16_t bms_sat;
    uint16_t ctrl_status;  //基本参数中的: 控制状态位

    //page3
    uint16_t cell_v_num;    // 电池串数
    uint8_t cell_num;
    uint16_t cell_vol[30];
    uint16_t max_vol;
    uint16_t min_vol;
    uint16_t max_vol_num;  //最高电压编号
    uint16_t min_vol_num;  //最低电压编号
};
extern struct lcd_display_info g_lcd_display_info;
extern struct lcd_display_info g_lcd_display_info_last;

/* LCD管理参数 */
typedef struct lcd_manager* lcd_manager_t;
struct lcd_manager{
    enum lcd_refresh_event lcd_event;
    enum lcd_display_page cur_page;         // 当前页面
    uint8_t touch_disable_flag;             // 允许200ms一次触摸
    uint8_t touch_disable_cnt;
    uint8_t lcd_busy_flag;                  // LCD忙
    uint8_t comm_disconnect_cnt;            // 10秒无通信则清除LCD显示的数据
    uint16_t sys_ctrl;                      // 系统参数中的: 系统控制参数, bit0->开MOS, bit1->关MOS
    uint8_t power_off_flag;                 // 无电流, 无操作65000s下电
    uint16_t power_off_cnt;
    uint16_t lcd_off_flag;
    uint16_t lcd_off_cnt;                   // 持续10分钟电流绝对值小于5A后，将PA3拉低，即关闭屏的背光
    void (*show_back)(enum lcd_display_page page);  // 显示背景
    void (*show_data)(enum lcd_display_page page);  // 显示数据
    uint8_t trickle_charging;               // 涓流充电
    uint8_t charge_full_cnt;                // 计时1min
    uint8_t charge_decrement_cnt;           // 递减次数
};
extern struct lcd_manager g_lcd_manager;

/* 电池SOC */
typedef struct lcd_battery* lcd_battery_t;
struct lcd_battery {
    uint16_t x;
    uint16_t y;
    uint16_t wide;
    uint16_t hight;
};
extern struct lcd_battery g_lcd_battery;

/* 电流表 */
typedef struct lcd_dial* lcd_dial_t;
struct lcd_dial {
    uint16_t center_x;
    uint16_t center_y;
    uint16_t radius;
    uint16_t current_angle;
    uint16_t last_angle;
    uint16_t offset_angle;
};
extern struct lcd_dial g_lcd_dial;

void _lcd_refresh_back(void);
void _lcd_refresh_data(void);
void lcd_task(void);
void lcd_draw_dial(lcd_dial_t dial);
void lcd_draw_picture(u16 xstr, u16 ystr, u16 xend, u16 yend, u8 *pic);	    //画图

#endif
